#include "ses_lcdDriver.h"



void lcdDriver_init(void) {
	DDR(DISPLAY_RD_PORT) |= (1 << DISPLAY_RD_PIN);
	DDR(DISPLAY_WR_PORT) |= (1 << DISPLAY_WR_PIN);
	DDR(DISPLAY_CS_PORT) |= (1 << DISPLAY_CS_PIN);
	DDR(DISPLAY_A0_PORT) |= (1 << DISPLAY_A0_PIN);
	DDR(DISPLAY_DATA_PORT) = 0xff;

	DISPLAY_RD_PORT |= (1 << DISPLAY_RD_PIN);
	DISPLAY_WR_PORT |= (1 << DISPLAY_WR_PIN);
}

void lcdDriver_selectController(uint8_t sel) {
	uint8_t i = sel;
	switch (i) {
	case 0:
		DISPLAY_CS_PORT &= ~(1 << DISPLAY_CS_PIN);
		break;
	case 1:
		DISPLAY_CS_PORT |= (1 << DISPLAY_CS_PIN);
		break;
	default:
		break;
	}
}

void lcdDriver_writeCommand(uint8_t data) {
	uint8_t command_input = data;
	DISPLAY_A0_PORT &= ~(1<<DISPLAY_A0_PIN);
	DISPLAY_WR_PORT &= ~(1 << DISPLAY_WR_PIN);
	_delay_us(1);
	DISPLAY_DATA_PORT = command_input;
	_delay_us(1);
	DISPLAY_WR_PORT |= (1 << DISPLAY_WR_PIN);
	DISPLAY_A0_PORT &= ~(1 << DISPLAY_A0_PIN);
}

void lcdDriver_writeData(uint8_t data) {
	uint8_t data_input = data;
	DISPLAY_A0_PORT |= (1<<DISPLAY_A0_PIN);
	DISPLAY_WR_PORT &= ~(1 << DISPLAY_WR_PIN);
	_delay_us(1);
	DISPLAY_DATA_PORT = data_input;
	_delay_us(1);
	DISPLAY_WR_PORT |= (1 << DISPLAY_WR_PIN);
	DISPLAY_A0_PORT &= ~(1 << DISPLAY_A0_PIN);
}

uint8_t lcdDriver_readStatus(void){
	uint8_t status;
	if ((PIN(DISPLAY_DATA_PORT) & (1 << 7)) == 0x00) {
		status = 0;
	} else {
		status = 1;
	}
	DISPLAY_A0_PORT &= ~(1 << DISPLAY_A0_PIN);
	return status;
}

